package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.tradewar.*;

import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class UserTradeWarInfoDaoImpl extends TempDataObjectDao<UserTradeWarInfo> {

	public Map<Long, UserTradeWarInfo> getUserTradeWarInfoMap() {
		String sql = "select * from t_u_trade_war_info";
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Long, UserTradeWarInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				UserTradeWarInfo data = parse(rs);
				dataMap.put(data.getUserId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public UserTradeWarInfo getUserTradeWarInfo(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_trade_war_info where userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserTradeWarInfo data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserTradeWarInfo data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_trade_war_info(userId,totalRandomBattleTimes,score,modifyTime,totalKillPatronsCount,totalTradeWarTimes,"
				+ "maxKillPatronsCount,totalBuyBuffTimes,totalPublishWantedTimes,totalBeWantedTimes,totalChallengeTimes,totalBeKillPatronsCount,"
				+ "lastBeAttackNotifyTime,activityId,crossScore,crossTradeWarMoney,dailyTrackTimes,dailyTrackResetTime) "
				+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setLong(i++, data.getTotalRandomBattleTimes());
			call.setLong(i++, data.getScore());
			call.setLong(i++, data.getModifyTime());
			call.setLong(i++, data.getTotalKillPatronsCount());
			call.setLong(i++, data.getTotalTradeWarTimes());
			call.setInt(i++, data.getMaxKillPatronsCount());
			call.setLong(i++, data.getTotalBuyBuffTimes());
			call.setLong(i++, data.getTotalPublishWantedTimes());
			call.setLong(i++,data.getTotalBeWantedTimes());
			call.setLong(i++, data.getTotalChallengeTimes());
			call.setLong(i++, data.getTotalBeKillPatronsCount());
			call.setLong(i++, data.getLastBeAttackNotifyTime());
			call.setInt(i++, data.getActivityId());
			call.setLong(i++, data.getCrossScore());
			call.setLong(i++, data.getCrossTradeWarMoney());
			call.setInt(i++, data.getDailyTrackTimes());
			call.setLong(i++, data.getDailyTrackResetTime());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserTradeWarInfo data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_trade_war_info SET totalRandomBattleTimes=?,score=?,modifyTime=?,totalKillPatronsCount=?,totalTradeWarTimes=?,"
				+ "maxKillPatronsCount=?,totalBuyBuffTimes=?,totalPublishWantedTimes=?,totalBeWantedTimes=?,totalChallengeTimes=?,totalBeKillPatronsCount=?,"
				+ "lastBeAttackNotifyTime=?,activityId=?,crossScore=?,crossTradeWarMoney=?,dailyTrackTimes=?,dailyTrackResetTime=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getTotalRandomBattleTimes());
			call.setLong(i++, data.getScore());
			call.setLong(i++, data.getModifyTime());
			call.setLong(i++, data.getTotalKillPatronsCount());
			call.setLong(i++, data.getTotalTradeWarTimes());
			call.setInt(i++, data.getMaxKillPatronsCount());
			call.setLong(i++, data.getTotalBuyBuffTimes());
			call.setLong(i++, data.getTotalPublishWantedTimes());
			call.setLong(i++, data.getTotalBeWantedTimes());
			call.setLong(i++, data.getTotalChallengeTimes());
			call.setLong(i++, data.getTotalBeKillPatronsCount());
			call.setLong(i++, data.getLastBeAttackNotifyTime());
			call.setInt(i++, data.getActivityId());
			call.setLong(i++, data.getCrossScore());
			call.setLong(i++, data.getCrossTradeWarMoney());
			call.setInt(i++, data.getDailyTrackTimes());
			call.setLong(i++, data.getDailyTrackResetTime());
			call.setLong(i++, data.getUserId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserTradeWarInfo parseData(ResultSet rs) throws SQLException {
		UserTradeWarInfo data = new UserTradeWarInfo();
		data.setUserId(rs.getLong("userId"));
		data.setTotalRandomBattleTimes(rs.getLong("totalRandomBattleTimes"));
		data.setScore(rs.getLong("score"));
		data.setModifyTime(rs.getLong("modifyTime"));
		data.setTotalKillPatronsCount(rs.getLong("totalKillPatronsCount"));
		data.setTotalTradeWarTimes(rs.getLong("totalTradeWarTimes"));
		data.setMaxKillPatronsCount(rs.getInt("maxKillPatronsCount"));
		data.setTotalBuyBuffTimes(rs.getLong("totalBuyBuffTimes"));
		data.setTotalPublishWantedTimes(rs.getLong("totalPublishWantedTimes"));
		data.setTotalBeWantedTimes(rs.getLong("totalBeWantedTimes"));
		data.setTotalChallengeTimes(rs.getLong("totalChallengeTimes"));
		data.setTotalBeKillPatronsCount(rs.getLong("totalBeKillPatronsCount"));
		data.setLastBeAttackNotifyTime(rs.getLong("lastBeAttackNotifyTime"));
		data.setActivityId(rs.getInt("activityId"));
		data.setCrossScore(rs.getLong("crossScore"));
		data.setCrossTradeWarMoney(rs.getLong("crossTradeWarMoney"));
		data.setDailyTrackTimes(rs.getInt("dailyTrackTimes"));
		data.setDailyTrackResetTime(rs.getLong("dailyTrackResetTime"));
		return data;
	}
}
